/**
 * 
 */
package com.shengruan.admin.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.shengruan.admin.vo.AuctionHistory;
import com.shengruan.framework.jdbc.BaseJdbc;
import com.shengruan.framework.jdbc.BeanListHandler;


public class AuctionHistoryJdbc extends BaseJdbc{

	/***
	 * 保存
	 * @param auctionHistory
	 */
	public void save(AuctionHistory auctionHistory) {
		Connection conn = this.getConnection();
		String sql = "insert into AuctionHistory(userId,productId,time,bidPrice,bidRound,ip) values(?,?,?,?,?,?)";
		Object[] pram = {auctionHistory.getUserId(),auctionHistory.getProductId(),auctionHistory.getTime(),auctionHistory.getBidPrice(),auctionHistory.getBidRound(),auctionHistory.getIp()};
		try {
			this.execute(conn, sql, pram);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
	}

	/***
	 * 根据商品ID删除
	 */
	public void delteByProductId(int productId)
	{

		Connection conn = this.getConnection();
		String sql = "delete from  AuctionHistory where productId=?";
		Object[] pram = {productId};
		try {
			this.execute(conn, sql, pram);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}

	}

    /**
     * 获得列表
     * @param currentPage
     * @param pageSize
     * @param productId
     * @return
     */
	public List<AuctionHistory> findWithPage(int currentPage,int pageSize,int productId) {
		Connection conn = this.getConnection();
		StringBuilder sql = new StringBuilder("SELECT av.*,p.name AS productName  FROM AuctionHistory_View av LEFT JOIN product p ON av.productId = p.id where av.productId = ?");
		List<AuctionHistory> auctionHistoryList = new ArrayList<AuctionHistory>();

		sql.append(" order by av.id desc limit ?,?");
		try {
			int start = (currentPage-1)*pageSize;
			auctionHistoryList = this.query(conn, sql.toString(), new BeanListHandler<AuctionHistory>(AuctionHistory.class),productId,start,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return auctionHistoryList;
	}

    /**
     *
     * @param productId
     * @return
     */
    public int getRows(int productId) {
        Connection conn = this.getConnection();
        StringBuffer sql = new StringBuffer("select count(av.id) FROM AuctionHistory_View av where productId = ?");
        Object[] pram = {productId};
        int count = 0;
        try {
            count = this.getRows(conn, sql.toString(), pram);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
        }
        return count;
    }
}
